<?php

namespace App\Model;


use App\Common\SysContext;
use App\Common\WX\Wx;
use App\ModelBase\__t_wx_reply;
use XWX\Common\H;

class t_wx_reply extends __t_wx_reply
{
    static function funcReplyApp($app_id, $key = '0')
    {
        $arr = SysContext::funcInsStatic()->db()
            ->where('reply_x_id', SysContext::funcInsStatic()->funcGetX())
            ->where('reply_app_id', $app_id)
            ->where('reply_key', $key)
            ->getOne(t_wx_reply::table());

        $entity = t_wx_reply::funcArrayToEntity($arr);


        return $entity;
    }


    /**
     * 获取跳转URL
     *
     * @return string
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    function getRedirect()
    {
        if ($this->reply_app_id != 'reply')
        {
            //非用户自定义-回复
            $wx = Wx::funcIns($this->funcGetX__config());
            return $wx->funcGetOAuthUrl($this->reply_app_id);
        }
        

        $url = $this->reply_redirect;
        if (H::funcStrIsNullOrEmpty($this->reply_redirect_type))
        {
            $wx = Wx::funcIns($this->funcGetX__config());
            $url = $wx->funcGetOAuthUrl($this->reply_redirect);
        }

        return $url;
    }
}